<?php 
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../DAO/ArticleDAO.php';
class ArticleService
{
	public function create(BaseContext $context, ServiceResult $result, ArticleDomain $articleDomain)
	{
		try
		{
			$articleDAO = new ArticleDAO($context);
			$articleDAO->create($articleDomain);
			$result->setSuccess();
		}
		catch (Exception $e)
		{
			$context->logException($e);
			$result->setException($e);
		}
	}

	public function update(BaseContext $context, ServiceResult $result, ArticleDomain $articleDomain)
	{
		try
		{
			$articleDAO = new ArticleDAO($context);
			$articleDAO->update($articleDomain);
			$result->setSuccess();
		}
		catch (Exception $e)
		{
			$context->logException($e);
			$result->setException($e);
		}
	}

	public function delete(BaseContext $context, ServiceResult $result, $oid)
	{
		try
		{
			$articleDAO = new ArticleDAO($context);
			$articleDAO->delete($oid);
			$result->setSuccess();
		}
		catch (Exception $e)
		{
			$context->logException($e);
			$result->setException($e);
		}
	}

	public function findByOid(BaseContext $context, ServiceResult $result, $oid)
	{
		try
		{
			$articleDAO = new ArticleDAO($context);
			$articleDomain = $articleDAO->findByOid($oid);
			$result->setSuccess();
			return $articleDomain;
		}
		catch (Exception $e)
		{
			$context->logException($e);
			$result->setException($e);
		}
	}

	public function findAll(BaseContext $context, ServiceResult $result)
	{
		try
		{
			$articleDAO = new ArticleDAO($context);
			$articleDomainArr = $articleDAO->findAll();
			$result->setSuccess();
			return $articleDomainArr;
		}
		catch (Exception $e)
		{
			$context->logException($e);
			$result->setException($e);
		}
	}
	
	//check Article code exist
	public function checkCodeExist (BaseContext $context, ServiceResult $result, $code) 
	{
		try 
		{
			$articleDao = new ArticleDAO($context);	
			$article = $articleDao->findByCode($code);
			if(!Tool::isEmpty($article))
				return true;
			else 
				return false;
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
	}

}
?>